<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>音乐考试系统 - 仪表板</title>
    <style>
        * { margin: 0; padding: 0; box-sizing: border-box; }
        body {
            font-family: 'Microsoft YaHei', Arial, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            padding: 20px;
        }
        .container { max-width: 1200px; margin: 0 auto; }
        .header {
            background: white;
            padding: 30px;
            border-radius: 15px;
            box-shadow: 0 10px 30px rgba(0,0,0,0.1);
            margin-bottom: 30px;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        .welcome { color: #333; font-size: 28px; font-weight: bold; }
        .username { color: #667eea; }
        .btn {
            display: inline-block;
            padding: 12px 25px;
            background: #ff6b6b;
            color: white;
            text-decoration: none;
            border-radius: 8px;
            font-weight: bold;
            transition: all 0.3s ease;
            margin-left: 10px;
        }
        .btn:hover { background: #ff5252; transform: translateY(-2px); }
        .section {
            background: white;
            padding: 30px;
            border-radius: 15px;
            box-shadow: 0 10px 30px rgba(0,0,0,0.1);
            margin-bottom: 30px;
        }
        .section-title { font-size: 24px; color: #333; margin-bottom: 25px; padding-bottom: 15px; border-bottom: 2px solid #f0f0f0; }
        .exam-grid { display: grid; gap: 20px; }
        .exam-card {
            background: #f8f9fa;
            padding: 25px;
            border-radius: 12px;
            border-left: 5px solid #667eea;
            transition: all 0.3s ease;
        }
        .exam-card:hover { transform: translateY(-3px); box-shadow: 0 8px 25px rgba(0,0,0,0.1); background: white; }
        .exam-title { font-size: 20px; color: #333; margin-bottom: 10px; font-weight: bold; }
        .exam-meta { color: #666; margin-bottom: 15px; font-size: 14px; }
        .start-btn, .action-btn {
            display: inline-block;
            padding: 10px 20px;
            color: white;
            text-decoration: none;
            border-radius: 6px;
            font-weight: bold;
            transition: all 0.3s ease;
            margin-right: 10px;
            margin-bottom: 5px;
        }
        .start-btn { background: #667eea; }
        .start-btn:hover { background: #5a6fd8; transform: translateY(-2px); }
        .delete-btn { background: #dc3545; }
        .delete-btn:hover { background: #c82333; transform: translateY(-2px); }
        .toggle-btn { background: #28a745; }
        .toggle-btn:hover { background: #218838; transform: translateY(-2px); }
        .create-btn { background: #17a2b8; }
        .create-btn:hover { background: #138496; transform: translateY(-2px); }
        .record-card { 
            background: #f8f9fa; 
            padding: 20px; 
            border-radius: 10px; 
            margin-bottom: 15px; 
            border-left: 4px solid #28a745;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        .score { font-size: 24px; font-weight: bold; color: #28a745; }
        .role-badge {
            display: inline-block;
            padding: 4px 12px;
            background: #667eea;
            color: white;
            border-radius: 20px;
            font-size: 12px;
            margin-left: 10px;
        }
        .teacher-badge { background: #dc3545; }
        .student-badge { background: #28a745; }
        .action-buttons { display: flex; gap: 10px; margin-top: 10px; flex-wrap: wrap; }
        .record-info { flex: 1; }
        .no-data { 
            text-align: center; 
            color: #666; 
            padding: 40px;
            font-style: italic;
        }
        .status-badge {
            display: inline-block;
            padding: 4px 8px;
            border-radius: 12px;
            font-size: 12px;
            margin-left: 10px;
        }
        .status-active { background: #28a745; color: white; }
        .status-inactive { background: #6c757d; color: white; }
        .delete-form { display: inline; }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <div>
                <h1 class="welcome">欢迎, <span class="username">{{ username }}</span>! 
                    <span class="role-badge {% if role == 'teacher' %}teacher-badge{% else %}student-badge{% endif %}">
                        {% if role == 'teacher' %}教师{% else %}学生{% endif %}
                    </span>
                </h1>
                <p style="color: #666; margin-top: 5px;">
                    {% if role == 'teacher' %}
                    👨‍🏫 教师管理面板 - 您可以创建和管理考试
                    {% else %}
                    👨‍🎓 学生考试面板 - 请选择要参加的考试
                    {% endif %}
                </p>
            </div>
            <div>
                <a href="/results" class="btn" style="background: #28a745;">查看成绩</a>
                {% if role == 'teacher' %}
                <a href="/teacher/create_exam" class="btn" style="background: #17a2b8;">创建新考试</a>
                {% endif %}
                <a href="/logout" class="btn">退出登录</a>
            </div>
        </div>
        
        {% if role == 'student' %}
        <!-- 学生视图 -->
        <div class="section">
            <h2 class="section-title">📝 可用考试</h2>
            <div class="exam-grid">
                {% for exam in exams %}
                <div class="exam-card">
                    <div class="exam-title">{{ exam.title }}</div>
                    <div class="exam-meta">
                        🏷️ 类型: {{ exam.exam_type or '常规测试' }} | 
                        📅 创建时间: {{ exam.created_at.strftime('%Y年%m月%d日') }} |
                        📊 题目数量: {{ exam.questions_count }}
                    </div>
                    <a href="/exam/{{ exam.id }}" class="start-btn">开始考试 →</a>
                </div>
                {% else %}
                <div class="no-data">
                    🎵 暂无可用考试，请联系老师添加考试内容
                </div>
                {% endfor %}
            </div>
        </div>
        
        {% if records %}
        <div class="section">
            <h2 class="section-title">📊 我的考试记录</h2>
            {% for record in records %}
            <div class="record-card">
                <div class="record-info">
                    <h3>{{ record.exam.title }}</h3>
                    <p>提交时间: {{ record.submitted_at.strftime('%Y-%m-%d %H:%M') }}</p>
                </div>
                <div class="score">{{ record.score }}分</div>
            </div>
            {% endfor %}
        </div>
        {% endif %}
        
        {% else %}
        <!-- 教师视图 -->
        <div class="section">
            <h2 class="section-title">👨‍🎓 学生考试记录</h2>
            {% if all_records %}
                {% for record in all_records %}
                <div class="record-card">
                    <div class="record-info">
                        <h3>{{ record.student.username }} - {{ record.exam.title }}</h3>
                        <p>提交时间: {{ record.submitted_at.strftime('%Y-%m-%d %H:%M') }}</p>
                    </div>
                    <div class="score">{{ record.score }}分</div>
                </div>
                {% endfor %}
            {% else %}
                <div class="no-data">
                    📊 暂无学生考试记录
                </div>
            {% endif %}
        </div>
        
        <div class="section">
            <h2 class="section-title">📚 我创建的考试</h2>
            <div class="exam-grid">
                {% for exam in teacher_exams %}
                <div class="exam-card">
                    <div class="exam-title">
                        {{ exam.title }}
                        <span class="status-badge {% if exam.is_active %}status-active{% else %}status-inactive{% endif %}">
                            {% if exam.is_active %}启用{% else %}禁用{% endif %}
                        </span>
                    </div>
                    <div class="exam-meta">
                        🏷️ 类型: {{ exam.exam_type or '常规测试' }} | 
                        📅 创建时间: {{ exam.created_at.strftime('%Y年%m月%d日') }} |
                        📊 题目数量: {{ exam.questions_count }} |
                        👥 提交次数: {{ exam.submissions_count }}
                    </div>
                    <div class="action-buttons">
                        <a href="/teacher/toggle_exam/{{ exam.id }}" class="action-btn toggle-btn">
                            {% if exam.is_active %}禁用考试{% else %}启用考试{% endif %}
                        </a>
                        <form class="delete-form" action="/teacher/delete_exam/{{ exam.id }}" method="POST" onsubmit="return confirm('确定要删除这个考试吗？这将删除所有相关记录！')">
                            <button type="submit" class="action-btn delete-btn">删除考试</button>
                        </form>
                    </div>
                </div>
                {% else %}
                <div class="no-data">
                    📝 您还没有创建任何考试，<a href="/teacher/create_exam" style="color: #667eea;">点击这里创建第一个考试</a>
                </div>
                {% endfor %}
            </div>
        </div>
        {% endif %}
    </div>
</body>
</html>